import pod
import time

remove, clear = True, False

db = pod.Db(file = 'mypod.sqlite3', remove = remove, clear = clear, very_chatty = False)

class Person(pod.Object):
    age    = pod.column.Int(index = False)
    name   = pod.column.String(index = False)
    
if remove or clear:
    db.execute(query = "CREATE TABLE regular_table (id INTEGER PRIMARY KEY, age INTEGER, name TEXT, weight REAL, phrase TEXT)")    
    db.commit(clear_cache = True)

db.vacuum()


class Person(pod.Object):
    age    = pod.column.Int(index = False)
    name   = pod.column.String(index = False)

def sql_insert(number_of_records):

    args = []

    for i in range(number_of_records): 
        name = 'Leonard_' + str(i)
        args.append((i,name,i+i**1.4, name + ', you got it, Toyota!',))    
    
    db.executemany(query = "INSERT INTO regular_table (age,name,weight,phrase) VALUES (?,?,?,?)", seq_of_args = args)
    
    
def pod_insert(number_of_records):

    for i in range(number_of_records):
        name = 'Leonard_' + str(i)
        Person(age = i)#, weight = i+i**1.4, phrase = name + ', you got it, Toyota!')
    
def sql_query_raw(number_of_records):  

    rows = db.execute('SELECT age FROM regular_table WHERE age < ?', (number_of_records,))
    for row in rows:
        int(row[0]) + 1

def pod_column_query(number_of_records):
    
    for peep in pod.Query(select = Person.age, where = Person.age < number_of_records):
        peep.age + 1

def pod_list_comp_query(number_of_records):
    
    for peep in [peep for peep in Person if peep.weight < (number_of_records+number_of_records**1.4)]:
        peep.weight + 1

def clear_person_table():
    db.execute('DELETE FROM __main___Person WHERE id > 0')
    db.commit(clear_cache = True)
    db.vacuum()
    time.sleep(0.25)


number = 10000
print '#H2: Speed of ' + str(number) + ' Inserts | Query on ' + str(2*number) + ' records'
print '>>> run(test_list = insert_tests, number_of_records = ' + str(number) + ')'
print 'Insert Test for ' + str(number) + ' inserts '
#run(test_list = [sql_insert, pod_insert, sql_insert, pod_insert], number_of_records = number, clear_cache = True)
#clear_person_table()
#run(test_list = [sql_insert_mock_0, pod_insert, sql_insert_mock_0, pod_insert], number_of_records = number, clear_cache = True)
#clear_person_table()
#run(test_list = [sql_insert_mock_1, pod_insert, sql_insert_mock_1, pod_insert], number_of_records = number, clear_cache = True)
#clear_person_table()
run(test_list = [sql_insert_mock_2, pod_insert, sql_insert_mock_2, pod_insert], number_of_records = number, clear_cache = True)
clear_person_table()
exit()
#
#for clear_cache in [True]:
#    for hits in [10, 100, 1000, 10000, 100000]:
#        if hits <= number: 
#            print '\n\nQuery (with a query designed to return ' + str(hits) + ' total hits): ' # and clear_cache = ' + str(clear_cache) + ':'
#            run(test_list = [sql_query_raw, pod_column_query, pod_list_comp_query, sql_query_raw, pod_column_query, pod_list_comp_query], 
#                number_of_records = hits, 
#                clear_cache = clear_cache)


#def profile():
#    run(test_list = [pod_insert], number_of_records = 10000, clear_cache = True)
#
#import cProfile
#cProfile.run('profile()')